/*
 * Lab4.cpp
 *
 *  Created on: 05.11.2013
 *      Author: ulgish
 */

#include <lab4/Lab4.h>
#include <unistd.h>
using DT::Reader;
using DT::Writer;
using DT::InformationFond;
void Lab4::go(int timeOfModelingInSecond)
{
	Reader::delayInSecond = delayReaderInSecond;
	Writer::delayInSecond = delayWriterInSecond;
	this->iFond->open();
	for (int i = 0; i < this->countOfReader; i++){
		readers[i]->start(this->iFond);
	};
	for (int i = 0; i < this->countOfWriter; i++){
		writers[i]->start(this->iFond);
	};
	sleep(timeOfModelingInSecond);
	this->iFond->close();
	sleep(2);
	for (int i = 0; i < this->countOfReader; i++){
		readers[i]->wait(NULL);
	};
	for (int i = 0; i < this->countOfWriter; i++){
		writers[i]->wait(NULL);
	}
	DT::writer.flush();

}

Lab4::Lab4(int countOfReader, int delayReaderInSecond, int countOfWriter, int delayWriterInSecond)
{
	this->countOfReader = countOfReader;
	this->countOfWriter = countOfWriter;
	this->delayReaderInSecond = delayReaderInSecond;
	this->delayWriterInSecond = delayWriterInSecond;
	this->iFond = new InformationFond();
	//readers
	readers = new Reader*[countOfReader];
	for (int i = 0; i < countOfReader; i++){
		readers[i] = new Reader();
	}
	//writers
	writers = new Writer*[countOfWriter];
	for (int i = 0; i < countOfWriter; i++){
		writers[i] = new Writer();
	}
}

Lab4::~Lab4() {
	// TODO Auto-generated destructor stub
	for (int i = 0; i < countOfReader; i++){
		delete readers[i];
	}
	delete []readers;
	for (int i = 0; i < countOfWriter; i++){
		delete writers[i];
	}
	delete []writers;
	delete iFond;
}
